resource "aws_vpc" "main" {
  cidr_block                       = "10.0.0.0/16"
  assign_generated_ipv6_cidr_block = true
  enable_dns_hostnames             = true
  enable_dns_support               = true

  tags = {
    Name = "main"
  }
}

resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "main"
  }
}

resource "aws_eip" "oathkeeper" {
  vpc               = true
  network_interface = aws_network_interface.public_1.id
}

resource "aws_subnet" "public_1" {
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
  vpc_id                  = aws_vpc.main.id
  tags = {
    Name = "public_1"
  }
}

resource "aws_subnet" "private_1" {
  cidr_block        = "10.0.254.0/24"
  vpc_id            = aws_vpc.main.id
  availability_zone = "eu-west-2a"
  tags = {
    Name = "private_1"
  }
}

resource "aws_subnet" "private_2" {
  cidr_block        = "10.0.253.0/24"
  vpc_id            = aws_vpc.main.id
  availability_zone = "eu-west-2b"
  tags = {
    Name = "private_2"
  }
}

resource "aws_security_group" "allow_tls" {
  name        = "allow_tls"
  description = "Allows TLS connections."
  vpc_id      = aws_vpc.main.id

  ingress = [{
    description      = "Allows TLS inbound traffic."
    from_port        = 443
    to_port          = 443
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  egress = [{
    description      = "Allows any outbound traffic."
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  tags = {
    Name = "allow_tls"
  }
}

resource "aws_security_group" "allow_aux_private" {
  name        = "allow_aux_private"
  description = "Allows Private aux connections."
  vpc_id      = aws_vpc.main.id

  ingress = [{
    description      = "Allows private aux inbound traffic."
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["10.0.0.0/16"]
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  egress = [{
    description      = "Allows any private outbound traffic."
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["10.0.0.0/16"]
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  tags = {
    Name = "allow_aux_private"
  }
}

resource "aws_security_group" "allow_any" {
  name        = "allow_any"
  description = "Allows any connections."
  vpc_id      = aws_vpc.main.id

  ingress = [{
    description      = "Allows inbound traffic."
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  egress = [{
    description      = "Allows any outbound traffic."
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  tags = {
    Name = "allow_all"
  }
}

resource "aws_security_group" "rds" {
  name        = "rds"
  description = "Allows Private aux connections."
  vpc_id      = aws_vpc.main.id

  ingress = [{
    description      = "Allows any private inbound traffic."
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["10.0.0.0/16"]
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  egress = [{
    description      = "Allows any private outbound traffic."
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["10.0.0.0/16"]
    ipv6_cidr_blocks = []
    prefix_list_ids  = []
    security_groups  = []
    self             = true
  }]

  tags = {
    Name = "rds"
  }
}

resource "aws_route_table" "main" {
  vpc_id = aws_vpc.main.id

  // Don't ask me why Terraform is forcing me to specify every value, I don't know why.
  route = [{
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.main.id

    carrier_gateway_id         = ""
    destination_prefix_list_id = ""
    egress_only_gateway_id     = ""
    instance_id                = ""
    ipv6_cidr_block            = ""
    local_gateway_id           = ""
    nat_gateway_id             = ""
    network_interface_id       = ""
    transit_gateway_id         = ""
    vpc_endpoint_id            = ""
    vpc_peering_connection_id  = ""
  }]

  tags = {
    Name = "main"
  }
}

resource "aws_route_table_association" "public_1" {
  subnet_id      = aws_subnet.public_1.id
  route_table_id = aws_route_table.main.id
}

resource "aws_route_table_association" "private_1" {
  subnet_id      = aws_subnet.private_1.id
  route_table_id = aws_route_table.main.id
}

resource "aws_network_interface" "public_1" {
  subnet_id       = aws_subnet.public_1.id
  security_groups = [aws_security_group.allow_tls.id, aws_security_group.allow_aux_private.id]
  private_ips     = ["10.0.1.254"]
  tags = {
    Name = "public_1"
  }
}

resource "aws_network_interface" "private_1" {
  subnet_id       = aws_subnet.public_1.id
  security_groups = [aws_security_group.allow_tls.id, aws_security_group.allow_aux_private.id]
  private_ips     = ["10.0.1.11"]
  tags = {
    Name = "private_1"
  }
}

resource "aws_network_interface" "private_2" {
  subnet_id       = aws_subnet.public_1.id
  security_groups = [aws_security_group.allow_tls.id, aws_security_group.allow_aux_private.id]
  private_ips     = ["10.0.1.12"]
  tags = {
    Name = "private_2"
  }
}
